<?php 
declare (strict_types = 1);
namespace app\api\controller;
use app\common\controller\Api;
use think\Request;
class BusinessPost extends Api
{
    protected $with = ['business','post_image'];
    //发帖
    public function add()
    {
        $this->businessCheckLogin();
        $post = array_filter(input('post.'));
        if(!empty($post)){
            //分类ID，内容
            if(!isset($post['post_class_id']) || !isset($post['content']))return msgerror('参数缺失');
            if(isset($post['images'])){
                $images = $post['images'];
                unset($post['images']);
            }
            $post['business_id'] = $this->businessinfo['id'];
            $this->model->save($post);
            $id = $this->model->id;
            if(isset($images)){
                $images = explode(',',$images);
                $arr = [];
                foreach ($images as $k=>$v){
                    $arr[] = ['business_post_id'=>$id,'image'=>$v,'type'=>2];
                }
                app('app\api\model\PostImage')->saveAll($arr);
            }
            return datasuccess([],'发布成功');
        }else{
            return msgerror('参数缺失');
        }
    }
    //详情接口
    public function view()
    {
        if (!input('post.id')) {
            return msgerror('缺少参数id');
        }
        $this->where[] = ['id', '=', input('post.id')];
        $list = $this->model->with($this->with)->where($this->where)->find()->toArray();

        urladd($list);
        $time = strtotime($list['update_time']);
        $list['update_time'] = date('Y-m-d',$time);
        if(!empty($this->with)){
            foreach ($this->with as $k=>$v) {
                if(is_string($v)){
                    urladd($list[$v]);
                }else{
                    urladd($list[$k]);
                }
            }
        }
        return datasuccess($list);
    }
}